# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=jsonschema
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.6.0
pkgrel=2
pkgdesc="An implementation of JSON Schema validation for Python (mingw-w64)"
arch=('any')
url="https://pypi.python.org/pypi/jsonschema"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
_dtoken="58/b9/171dbb07e18c6346090a37f03c7e74410a1a56123f847efed59af260a298"
source=("https://pypi.python.org/packages/${_dtoken}/${_realname}-${pkgver}.tar.gz")
sha256sums=('6ff5f3180870836cae40f06fa10419f557208175f13ad7bc26caa77beb1f6e02')

all_build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete
}

package_python3-jsonschema() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_python2-jsonschema() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE

  cd "${pkgdir}${MINGW_PREFIX}"/bin
  for _f in *; do
    mv ${_f} ${_f/%.*/}2${_f/${_f/%.*/}/}
  done
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_mingw-w64-i686-python2-jsonschema() {
  package_python2-jsonschema
}

package_mingw-w64-i686-python3-jsonschema() {
  package_python3-jsonschema
}

package_mingw-w64-x86_64-python2-jsonschema() {
  package_python2-jsonschema
}

package_mingw-w64-x86_64-python3-jsonschema() {
  package_python3-jsonschema
}
